home *** CD-ROM | disk | FTP | other *** search
- /*
- * (C) 1992 SixxHeads Software
- * All Rights Reserved
- *
- * This stuff is collected from all over
- * the known universe. If you wrote something
- * and put the source in the public domain, it's
- * probably here.
- *
- * <Revision History>
- * 04/28/92 smz Created.
- * 05/16/92 smz added GetFileModDate()
- */
-
- #include "Utils.h"
- #include <varargs.h>
- #include <stdlib.h>
- #include <string.h>
- #include <Finder.h>
- #include <GestaltEqu.h>
- #include <Traps.h>
-
- /* Globally global globals
- */
-
- char CrsrCouple : 0x8cf;
- char CrsrNew : 0x8ce;
- Point MTemp : 0x828;
- Point RawMouse : 0x82c;
- Point Mouse : 0x830;
-
- /*
- * Local Globals
- */
- #define MAXPORTSTACK 8
-
- GrafPtr portStack[MAXPORTSTACK];
- short curPortSP;
-
- Boolean IsFinderRunning()
- {
- return pstrequal(CurApName, FinderName);
- }
-
- Boolean HaveGestalt()
- {
- return NGetTrapAddress(_GestaltDispatch, OSTrap) != NGetTrapAddress(_Unimplemented, OSTrap);
- }
-
- Boolean System7Running()
- {
- long response;
- if (HaveGestalt())
- if (Gestalt(gestaltSystemVersion, &response) == noErr)
- return response >= 0x0700;
- return false;
- }
-
- /***********************************************************************************************/
-
- Boolean pstrequal(reg unsigned char *p1, reg unsigned char *p2)
- {
- reg short len = *p1++;
-
- if (len != *p2++)
- return false;
-
- while (len--)
- if (*p1++ != *p2++)
- return false;
-
- return true;
- }
-
- Handle MoveToSystemHeap(Handle hRes)
- {
- #if __option(a4_globals)
- reg long saveZone = *(long*) TheZone;
- #endif
- reg Handle hNew;
- reg short size;
-
- size = GetHandleSize(hRes);
-
- #if __option(a4_globals)
- *(long *) TheZone = *(long *) SysZone;
- #endif
-
- hNew = NewHandle(size);
-
- #if __option(a4_globals)
- *(long *) TheZone = saveZone;
- #endif
-
- if (hNew == nil) {
- #ifdef DEBUG
- DebugStr("\pCouldn't make handle in system heap!");
- #endif
- return nil;
- }
-
- BlockMove(*hRes, *hNew, size);
-
- ReleaseResource(hRes);
-
- return hNew;
- }
-
- Handle CopyToSystemHeap(Handle hMem)
- {
- reg Handle hNew;
- reg short size;
-
- size = GetHandleSize(hMem);
-
- hNew = NewHandleSys(size);
-
- if (hNew == nil) {
- #ifdef DEBUG
- DebugStr("\pCouldn't make handle in system heap!");
- #endif
- return nil;
- }
-
- BlockMove(*hMem, *hNew, size);
-
- return hNew;
- }
-
- #if 0
- void dprintf(const char* format, ...)
- {
- va_list args;
- static char str[256];
- int count;
-
- va_start(args, format);
- count = vsprintf(str, format, args);
- va_end(args);
- DebugStr(c2pstr(str));
- }
-
- void nprintf(const char* format, ...)
- {
- va_list args;
- char str[256];
- char buff[256];
- int count;
- Boolean savePatchesInstalled = thePatchesInstalled;
-
- va_start(args, format);
- count = vsprintf(str, format, args);
- va_end(args);
-
- sprintf(buff, "%s: %s", thePatchName, str);
- ParamText(c2pstr(buff), nil, nil, nil);
- Alert(cFinderAlert, nil);
-
- thePatchesInstalled = savePatchesInstalled;
- }
- #endif
-
- //////////////////////////////////////////////////////////////////
-
- typedef char smallIconBits[32];
- typedef smallIconBits **hdlsmalliconbits;
-
- static char unknownBits[] = {
- 0x00, 0x00, 0x07, 0x80, 0x0F, 0xC0, 0x18, 0x60, 0x18,
- 0x60, 0x18, 0x60, 0x00, 0x60, 0x00, 0xC0, 0x00, 0xC0,
- 0x01, 0x80, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00,
- 0x00, 0x03, 0x00, 0x03, 0x00
- };
-
- void PlotSicn(Handle hSicn, short ix, Point loc)
- {
- Rect dstRect;
- BitMap sicnBits;
- GrafPtr curPort;
- reg hdlsmalliconbits hbits = (hdlsmalliconbits) hSicn;
- short ctSicns = 0;
-
- if (hSicn) {
- ctSicns = GetHandleSize(hSicn) / sizeof(smallIconBits);
- HLock(hSicn);
- }
-
- if (ix < ctSicns && hSicn)
- sicnBits.baseAddr = (Ptr) &(*hbits)[ix];
- else
- sicnBits.baseAddr = unknownBits;
-
- sicnBits.rowBytes = 2;
- sicnBits.bounds.top = sicnBits.bounds.left = 0;
- sicnBits.bounds.bottom = sicnBits.bounds.right = 16;
-
- dstRect = sicnBits.bounds;
- OffsetRect(&dstRect, loc.h, loc.v - 12);
-
- GetPort(&curPort);
-
- CopyBits(&sicnBits, &curPort->portBits, &sicnBits.bounds, &dstRect, srcOr, nil);
-
- if (hSicn)
- HUnlock(hSicn);
- }
-
- void PushPort(GrafPtr pPort)
- {
- GetPort(&portStack[curPortSP++]);
- #ifdef DEBUG
- if (curPortSP >= MAXPORTSTACK)
- DebugStr("\ptoo many pushports!");
- #endif
- SetPort(pPort);
- }
-
- void PopPort()
- {
- #ifdef DEBUG
- if (curPortSP <= 0) {
- DebugStr("\ptoo many popports!");
- return;
- }
- #endif
-
- SetPort(&portStack[--curPortSP]);
- }
-
- //////////////////////////////////////////////////////////////////
-
- Boolean IsTargetRunning(long code, ProcessSerialNumber *r)
- {
- OSErr result;
- ProcessSerialNumber p;
- ProcessInfoRec info;
- FSSpec procSPec;
- Str255 procName;
-
- if (*(char *) 0x910 == -1)
- return false;
-
- p.highLongOfPSN = 0;
- p.lowLongOfPSN = kNoProcess;
-
- while (GetNextProcess(&p) == noErr) {
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = (void*) &procName;
- info.processAppSpec = &procSPec;
- result = GetProcessInformation(&p, &info);
- if (result != noErr)
- return false;
- if (info.processSignature == code) {
- if (r)
- *r = p;
- return true; /* frontier is running! */
- }
- }
- return false;
- }
-
- void BringAppToFront(reg ProcessSerialNumber *p)
- {
- ProcessSerialNumber save;
- GetFrontProcess(&save);
- SetFrontProcess(p);
- *p = save;
- }
-
- //////////////////////////////////////////////////////////////////
-
- void copystring (Str255 source, Str255 dest)
- {
- /*
- create a copy of source in dest. copy the length byte and
- all the characters in the source string.
-
- assume the strings are pascal strings, with the length byte in
- the first character of the string.
- */
-
- reg short i, len;
-
- len = (short) source [0];
-
- for (i = 0; i <= len; i++)
- dest [i] = source [i];
- } /*copystring*/
-
- void cstring2pstring(unsigned char *cSource, Str255 dest)
- {
- reg short len = strlen((char *) cSource);
- reg char *pDest = (char *) &dest[1];
- dest[0] = len;
- while (len--)
- *pDest++ = *cSource++;
- }
-
- Boolean pushstring(Str255 bssource, Str255 bsdest)
- {
- reg short lensource = bssource [0];
- reg short lendest = bsdest [0];
- reg char *psource, *pdest;
-
- if ((lensource + lendest) > 255)
- return (false);
-
- pdest = (char *) bsdest + (char) lendest + 1;
-
- psource = (char *) bssource + 1;
-
- bsdest [0] += (char) lensource;
-
- while (lensource--) *pdest++ = *psource++;
-
- return (true);
- } /*pushstring*/
-
-
- Boolean pushlong(long num, Str255 bsdest)
- {
- Str255 bsint;
-
- NumToString (num, bsint);
-
- return (pushstring (bsint, bsdest));
- } /*pushlong*/
-
- void pushchar(char c, Str255 s)
- {
- s[++*s] = c;
- }
-
- short String2Short(Str255 s)
- {
- long tmp;
- StringToNum(s, &tmp);
- return (short) tmp;
- }
-
- Boolean String2Boolean(Str255 tmp)
- {
- if (EqualString(tmp, "\p1", false, false))
- return true;
- if (EqualString(tmp, "\ptrue", false, false))
- return true;
- return false;
- }
-
- Boolean stringtotext(Str255 s, Handle *htext)
- {
- reg long len;
- reg Handle h;
-
- len = s [0];
-
- h = NewHandle (len);
-
- if (h == nil)
- return (false);
-
- BlockMove (&s [1], *h, len);
-
- *htext = h;
-
- return (true);
- } /*stringtotext*/
-
-
- Boolean texttostring(Handle htext, Str255 s)
- {
- long len;
-
- len = GetHandleSize (htext);
-
- if (len > 255)
- len = 255;
-
- BlockMove (*htext, &s [1], len);
-
- s [0] = (char) len;
-
- return (true);
- } /*texttostring*/
-
-
- Handle GetFullPath(short vRefNum, unsigned long parentID, Str255 baseName)
- {
- CInfoPBRec block;
- Str255 directoryName;
- OSErr err;
- Handle hFileName;
-
- #if __option(a4_globals)
- hFileName = NewHandleSys(0);
- #else
- hFileName = NewHandle(0);
- #endif
- if (hFileName == nil)
- return nil;
-
- if (parentID != 1) { // not a disk
- *directoryName = 0;
-
- block.dirInfo.ioNamePtr = (void *) directoryName;
- block.dirInfo.ioDrParID = parentID;
-
- do {
- block.dirInfo.ioVRefNum = vRefNum;
- block.dirInfo.ioFDirIndex = -1;
- block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
-
- err = PBGetCatInfo(&block,false);
- if (err != noErr)
- goto errorExit;
-
- pushchar(':', directoryName);
- Munger(hFileName, 0, nil, 0, &directoryName[1], *directoryName);
- } while (block.dirInfo.ioDrDirID != fsRtDirID);
- }
-
- if (PtrAndHand(&baseName[1], hFileName, *baseName) != noErr)
- goto errorExit;
-
- if (parentID == 1) { // it's a disk, add a ":"
- if (PtrAndHand(":", hFileName, 1) != noErr)
- goto errorExit;
- }
-
- return hFileName;
-
- errorExit:
- DisposeHandle(hFileName);
- return nil;
- }
-
- static Boolean LookForDuplicates(Handle **hFileList, short ctEntries, Handle hNewName)
- {
- reg short i;
- reg short nameLen = GetHandleSize(hNewName);
- reg Handle *pFileList = *hFileList;
-
- if (nameLen <= 0)
- return true; /* skip blank ones */
-
- for (i = 0; i < ctEntries; i++) {
- reg Handle hMem = *pFileList++;
-
- if (hMem == nil) {
- #ifdef DEBUG
- DebugStr("\pbad handle in lookforduplicates!");
- #endif
- return true;
- }
-
- if (GetHandleSize(hMem) != nameLen)
- continue;
-
- if (! memcmp(*hMem, *hNewName, nameLen))
- return true;
- }
- return false;
- }
-
- void PushFullPath(CInfoPBPtr info, Handle **hFileList)
- {
- if (hFileList) {
- reg short ct = GetHandleSize(hFileList) >> 2;
- Handle newName;
-
- newName = GetFullPath(info->hFileInfo.ioVRefNum, info->dirInfo.ioDrParID, info->hFileInfo.ioNamePtr);
-
- if (newName == nil)
- return;
-
- if (ct > 0 && LookForDuplicates(hFileList, ct, newName)) {
- DisposeHandle(newName);
- return;
- }
-
- Munger(hFileList, 0, nil, 0, &newName, sizeof(Handle));
- }
- }
-
- /*
- * if the string fits inside the given number of pixels, fine -- do nothing
- * and return.
- *
- * if not, return a string that does fit, with ellipses representing the
- * deleted characters. ellipses are generated by pressing option-semicolon.
- */
- void ellipsize(Str255 s, short width)
- {
- reg char len;
- reg short newwidth;
-
- if ((newwidth = StringWidth (s)) <= width) /*nothing to do, the string fits*/
- return;
-
- len = s [0]; /* current length in characters*/
-
- width -= CharWidth ('…'); /* subtract width of ellipses*/
-
- do { /*until it fits (or we run out of characters)*/
-
- newwidth -= CharWidth (s [len]);
-
- --len;
- } while ((newwidth > width) && (len != 0));
-
- ++len; /*make room for the ellipses*/
-
- s [len] = '…';
-
- s [0] = (char) len;
- } /*ellipsize*/
-
-
- /*
- * draw the string in the current font, size and style, centered inside
- * the indicated rectangle.
- */
- void centerstring(Rect r, Str255 s)
- {
- reg short rh = r.bottom - r.top;
- reg short rw = r.right - r.left;
- reg short h, v;
- FontInfo fi;
-
- GetFontInfo(&fi);
- ellipsize(s, rw); /*make sure it fits inside the rectangle, width-wise*/
- h = r.left + ((rw - StringWidth (s)) / 2);
- v = r.top + ((rh - (fi.ascent + fi.descent)) / 2) + fi.ascent;
- MoveTo(h, v);
- ClipRect(&r);
- DrawString(s);
- } /*centerstring*/
-
-
- void setfontsizestyle (short fontnum, short fontsize, short fontstyle)
- {
- TextFont (fontnum);
- TextSize (fontsize);
- TextFace (fontstyle);
- } /*setfontsizestyle*/
-
-
- OSType GetProcessCreator(void)
- {
- #if __option(a4_globals)
- return 'ƒMNU';
- #else
- /*
- get the 4-character creator identifier for the application we're running
- inside of.
- */
-
- ProcessSerialNumber psn;
- ProcessInfoRec info;
-
- if (*(char *) 0x910 == -1)
- return 'XxXX';
-
- GetCurrentProcess(&psn);
- info.processInfoLength = (long) sizeof(info);
- info.processName = nil;
- info.processAppSpec = nil;
- GetProcessInformation(&psn, &info);
- return (info.processSignature);
- #endif
- } /*GetProcessCreator*/
-
- OSType GetFrontProcessCreator()
- {
- /*
- get the 4-character creator identifier for the application we're running
- inside of.
- */
-
- ProcessSerialNumber psn;
- ProcessInfoRec info;
-
- if (*(char *) 0x910 == -1)
- return 'INIT';
-
- GetFrontProcess(&psn);
- info.processInfoLength = (long) sizeof(info);
- info.processName = nil;
- info.processAppSpec = nil;
- GetProcessInformation(&psn, &info);
- return (info.processSignature);
- }
-
- //////////////////////////////////////////////////////////////////
-
- Boolean NewFileRecord(Str255 s, long creator, long type, FileRecord* info)
- {
- info->itsDirKnown = false;
- info->itsVRefNum = -1;
- info->itsDirID = -1;
- info->itsRefNum = -1;
- info->itsCreator = creator;
- info->itsType = type;
- info->itsOpen = false;
- copystring(s, info->itsName);
- return true;
- }
-
- static Boolean CheckPrefsDir(FileRecord *info)
- {
- if (info->itsDirKnown == false) {
- if (FindFolder(kOnSystemDisk, kPreferencesFolderType, true, &info->itsVRefNum, &info->itsDirID) == noErr)
- info->itsDirKnown = true;
- }
- return info->itsDirKnown;
- }
-
- Boolean OpenPrefsFile(FileRecord *info)
- {
- if (! CheckPrefsDir(info)) {
- #ifdef DEBUG
- DebugStr("\pcan't find prefs folder!");
- #endif
- return false;
- }
- info->itsRefNum = HOpenResFile(info->itsVRefNum, info->itsDirID, info->itsName, fsRdWrPerm);
- if (info->itsRefNum == -1)
- return false;
- info->itsOpen = true;
- info->itsModDate = GetFileModDate(info);
- return true;
- }
-
- Boolean ClosePrefsFile(FileRecord* info)
- {
- if (info->itsOpen) {
- CloseResFile(info->itsRefNum);
- info->itsOpen = false;
- return true;
- }
- return false;
- }
-
- OSErr CreatePrefsFile(FileRecord* info)
- {
- OSErr err;
-
- if (info->itsOpen)
- ClosePrefsFile(info);
- if (! CheckPrefsDir(info)) {
- #ifdef DEBUG
- DebugStr("\pcan't find prefs folder");
- #endif
- return false;
- }
- err = Create(info->itsName, info->itsVRefNum, info->itsCreator, info->itsType);
- if (err == noErr)
- info->itsModDate = GetFileModDate(info);
- return err;
- }
-
- unsigned long GetFileModDate(FileRecord* info)
- {
- HFileInfo fileInfo;
- short ctItems;
-
- fileInfo.ioCompletion = nil;
- fileInfo.ioNamePtr = (void*) &info->itsName;
- fileInfo.ioVRefNum = info->itsVRefNum;
- fileInfo.ioFDirIndex = 0;
- fileInfo.ioDirID = info->itsDirID;
-
- if (PBGetCatInfo(&fileInfo, false) != noErr)
- return 0; /* error */
-
- return fileInfo.ioFlMdDat;
- }
-
- Boolean PrefsFileChanged(FileRecord* info)
- {
- return info->itsModDate != GetFileModDate(info);
- }
-
- //////////////////////////////////////////////////////////////////
-
- MenuHandle FindInstalledMenu(short menuID, short *leftEdge)
- {
- if (theMenuList != nil) {
- short ctMenus = (GetHandleSize(theMenuList) - 6) / sizeof(PerMenu);
- short i;
- PerMenu *p = (**theMenuList).permenu;
- for (i = 0; i < ctMenus; i++, ++p) {
- if (p->itsHandle != nil)
- if ((**p->itsHandle).menuID == menuID) {
- *leftEdge = p->itsLeftEdge;
- return p->itsHandle;
- }
- }
- }
- return nil;
- }
-
- Boolean IsItemEnabled(unsigned long theItem)
- {
- reg short menuID = theItem >> 16;
- reg short item = theItem & 0xffff;
- short leftEdge;
- reg MenuHandle foundMenu = FindInstalledMenu(menuID, &leftEdge);
-
- if (foundMenu == nil)
- return false;
-
- return ((**foundMenu).enableFlags & (1 << item)) != 0;
- }
-
- void SetMouse(Point newLoc)
- {
- CrsrCouple = false;
- CrsrNew = true;
- MTemp = RawMouse = Mouse = newLoc;
- CrsrCouple = true;
- }
-
- void MemZero(void *pp, short l)
- {
- asm {
- move.l pp,a0
- move.w l,d0
- sub.w #1,d0
- beq.s @1
- @0
- clr.b (a0)+
- dbne.w d0,@0
- @1
- }
- }